package voidgame.core;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame implements ActionListener{
	
	NameGenerator ng;
	Galaxy galaxy;
	Player player;
	int gameMode;
	int turn;
	
	int[] mapsizes;
	
	boolean modeChange;
	
	JPanel containerPanel;	
	JPanel mainPanel;
	JPanel consolePanel;
	
	public static final int GAMEMODETEST = -1;
	public static final int GAMEMODENAVIGATE = 0;
	public static final int GAMEMODEINSPECT = 1;
	public static final int GAMEMODETRADE = 2;
	public static final int GAMEMODESTATUS = 3;
	
	public static final int MAPSIZEX = 55;
	public static final int MAPSIZEY = 25;
	
	public static final int GALAXYSIZE = 11;
	
	public Game(){
		
		ng = new NameGenerator();
		galaxy = new Galaxy(this, Game.GALAXYSIZE);
		player = new Player(this);
		gameMode = Game.GAMEMODENAVIGATE;
		modeChange = false;
		turn=1;
		
		
		containerPanel=new JPanel(new BorderLayout());
		containerPanel.setBackground(Color.DARK_GRAY);
		mainPanel = new JPanel();
		consolePanel = new JPanel();
				
        
		mainPanel = new JPanel();
		mainPanel.setPreferredSize(new Dimension(750,768));
		containerPanel.add(mainPanel,BorderLayout.WEST,0);
		
		consolePanel = new JPanel(new BorderLayout());
		consolePanel.setPreferredSize(new Dimension(250,768));
		containerPanel.add(consolePanel,BorderLayout.EAST,1);
		
		                
        NavigatePanel nav = new NavigatePanel(this);
        nav.setPreferredSize(new Dimension(726,726));
        mainPanel.add(nav,0);
        
        TestPanel test = new TestPanel(Color.RED);
        test.setPreferredSize(new Dimension(726,726));
        mainPanel.add(test,1);
        
        mainPanel.getComponent(1).setVisible(false);
        
        
        ControlPanel control = new ControlPanel(this);
		control.setPreferredSize(new Dimension(256,300));
		consolePanel.add(control,BorderLayout.NORTH,0);
		
		DetailPanel detail = new DetailPanel(this);
		detail.setPreferredSize(new Dimension(256,300));
		consolePanel.add(detail,BorderLayout.CENTER,0);
		
		RadarPanel radar = new RadarPanel(this);
		radar.setPreferredSize(new Dimension(256,100));
		consolePanel.add(radar,BorderLayout.SOUTH,0);
        
        
        add(containerPanel,0);
               
        
		
		setTitle("Void");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1024, 768);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        
        addKeyListener(new KeyMonitor());
		
	}
	
	
	public void draw(){
		
		switch(gameMode){
			case Game.GAMEMODENAVIGATE:{
				if(modeChange){
					mainPanel.getComponent(1).setVisible(false);
					mainPanel.getComponent(0).setVisible(true);
					mainPanel.repaint();
				}
				mainPanel.getComponent(0).repaint();
				break;
			}
			case Game.GAMEMODEINSPECT:{
				if(modeChange){
					mainPanel.getComponent(1).setVisible(false);
					mainPanel.getComponent(0).setVisible(true);
					mainPanel.repaint();
				}
				mainPanel.getComponent(0).repaint();
				break;
			}
			case Game.GAMEMODETEST:{
				if(modeChange){
					mainPanel.getComponent(0).setVisible(false);
					mainPanel.getComponent(1).setVisible(true);
					mainPanel.repaint();
				}
				mainPanel.getComponent(1).repaint();
				break;
			}
			
			
		}
		
		consolePanel.getComponent(0).repaint();
		consolePanel.getComponent(1).repaint();
		consolePanel.getComponent(2).repaint();
		
		
	}
	
	
	public void keypress(KeyEvent k){
		
		System.out.println(k.getKeyCode()+" "+k.getKeyChar());
		
		modeChange=false;
		
		switch(gameMode){
			case Game.GAMEMODENAVIGATE:{
				switch(k.getKeyCode()){
					case 10: endTurn();draw();break;
					case 37: player.move(0);draw();break;
					case 38: player.move(1);draw();break;
					case 39: player.move(2);draw();break;
					case 40: player.move(3);draw();break;
					case 69: player.enterWormHole();draw();break;
					case 73: changeMode(Game.GAMEMODEINSPECT);draw();break;
					case 84: changeMode(Game.GAMEMODETEST);draw();break;
				}
				break;
			}
			case Game.GAMEMODEINSPECT:{
				NavigatePanel nav = (NavigatePanel)mainPanel.getComponent(0);
				switch(k.getKeyCode()){
					case 10: endTurn();draw();break;
					case 78: changeMode(Game.GAMEMODENAVIGATE);draw();break;
					case 37: nav.moveInspect(0);draw();break;
					case 38: nav.moveInspect(1);draw();break;
					case 39: nav.moveInspect(2);draw();break;
					case 40: nav.moveInspect(3);draw();break;
					case 84: changeMode(Game.GAMEMODETEST);draw();break;
				}
				break;
			}
			case Game.GAMEMODETEST:{
				switch(k.getKeyCode()){
					case 78: changeMode(Game.GAMEMODENAVIGATE);draw();break;
					case 73: changeMode(Game.GAMEMODEINSPECT);draw();break;
				}
				break;
			}
		}
	}
	
	public void changeMode(int mode){
		gameMode=mode;
		modeChange=true;
	}
	
	
	public void endTurn(){
		player.endTurn();
		turn+=1;
	}
	
	
	
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("Action Event");
		
	}
	

	public static void main(String args[]){
		Game g = new Game();
		g.galaxy.printQuickDesc();
			
	}
	
	private class KeyMonitor extends KeyAdapter {
	    
	    public void keyPressed(KeyEvent event) {
	        keypress(event);
	    }
	}

	
	
	
}